/**
 * 
 */
package com.bitBusy.manageyourriches.utilityClasses.pickers;


import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.bitBusy.manageyourriches.database.managers.DateTimeManager;

/**
 * Class to set a date from date picker
 * @author SumaHarsha
 * 
 *
 */
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
		
	
	/**Listeners*/
	private DateChangeListener myListener;

	/**Reference to the set day */
	private static int myDay;
	
	/**Reference to the set month*/
	private static int myMonth;
	
	/**Reference to the set year*/
	private static int myYear;
	
	/**Current time*/
	private static Time now = new Time();
	
	/**Object where the date is to be written*/
	private TextView myView;
	
		
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// Use the current date as the default date in the picker
		now.setToNow();
		int dayOfMonth = now.monthDay;
		int month = now.month;
		int year = now.year;		

		// Create a new instance of DatePickerDialog and return it
		return new DatePickerDialog(getActivity(), this, year, month, dayOfMonth);
	}
		


	public void onDateSet(DatePicker view, int year, int month, int day) {
		now.setToNow();
		myDay = day;
		myMonth = month+1;
		myYear = year;
		setChangedDateOnView();
		notifyListeners();
	}   
	
	/** Method to set the changed date on the given View object*/
	private void setChangedDateOnView()
	{
		if (myView != null)
		{
			if (myView instanceof TextView)
			{
				((TextView)myView).setText(getDate()); 
			}
			else if (myView instanceof Button)
			{
				((Button)myView).setText(getDate()); 
			}
			else if (myView instanceof EditText)
			{
				((EditText)myView).setText(getDate()); 
			}
		}
	}
	
	/** Method to get new date 
	 * @return date in StringBuilder format*/
	private StringBuilder getDate()
	{
		return new StringBuilder().append(myMonth).append(DateTimeManager.DATESPLITTER).append(myDay).append(DateTimeManager.DATESPLITTER).append(myYear);
	}
	
	
	
	/**Method to specify Object to which new date has to be set*
	 * @param the View object
	 * @return boolean indicating if successfully set*/
	public boolean setOnObject(TextView the_view)
	{
		if( the_view != null)
			{
				if (the_view instanceof TextView || the_view instanceof Button || the_view instanceof EditText)
				{
					myView = the_view;
				return true;
			}
		}
		return false;
	}
	
	/**Method to add listener
	 * @param the listener
	 */
	public void addListener(DateChangeListener the_listener)
	{
		if (the_listener != null)
		{
			myListener = the_listener;
		}
	}
	
	/**Method to notify listeners of change*/
	private void notifyListeners()
	{
		if (myListener != null)
		{
			myListener.dateHasChanged(myView);
		}
	}
}
